//
//  ThreeViewController.m
//  WuliHanhan
//
//  Created by chen tang on 16/2/25.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "ThreeViewController.h"
#import "Header.h"


#import "CameraViewController.h"
#import "PhotoSelectViewController.h"
#import "BoxDownLoadViewController.h"
#import "TakingpicturesViewController.h"


#import "FMDB.h"
#import "PostUploadImageDataBase.h"




#import "UserEditeImageViewController.h"



#define KX          WithView/3.0
#define textPX      119.0/WithView

@interface ThreeViewController ()<cameraDeledate, UIScrollViewDelegate, PhotoSelectViewControllerDelegate, BoxDownLoadViewControllerDelegate, TakingpicturesViewControllerDelegate>
{
    UIView *bottomView;
    UIButton *button1;
    UIButton *button2;
    UIButton *button3;
    
    float currentX;
    NSMutableArray *PhotoImageArray;
    NSMutableArray *PhotoIDArray;
    
    NSMutableArray *boxImageArray;
    NSMutableArray *boxIDArray;
}


@property (nonatomic, strong) UIViewController *currentVC;
@property (nonatomic, strong) UIScrollView *mainScrollview;

@property (nonatomic, strong) NSMutableArray *mutableArray;
@property (nonatomic, strong) NSMutableArray *mutableIDArray;


@end





@implementation ThreeViewController



sloganIntence(ThreeViewController)







- (void)loadView
{
    [super loadView];
    
    
    
    PhotoImageArray = [NSMutableArray new];
    PhotoIDArray = [NSMutableArray new];
    
    boxImageArray = [NSMutableArray new];
    boxIDArray = [NSMutableArray new];
    
    _mutableArray = [NSMutableArray new];
    _mutableIDArray = [NSMutableArray new];
    
    
    self.automaticallyAdjustsScrollViewInsets = false;
    
    [self.view setBackgroundColor:[UIColor blackColor]];
    
    [self.navigationController setNavigationBarHidden:YES];
    
    
    _mainScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WithView, heightView-20.0)];
    [self.view addSubview:_mainScrollview];
    [_mainScrollview setPagingEnabled:YES];
    [_mainScrollview setContentSize:CGSizeMake(WithView*3, heightView-20.0)];
    
    
    button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, heightView-48, KX, 48)];
    [button1 setBackgroundColor:[UIColor whiteColor]];
    [button1 setTitleColor:uicolor(34, 34, 34) forState:UIControlStateNormal];
    [button1 setTitle:@"照片" forState:UIControlStateNormal];
    [button1.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
//    [button1.titleLabel setTransform:CGAffineTransformMakeScale(2.0, 2.0)];
    [self.view addSubview:button1];
//
    button2 = [[UIButton alloc] initWithFrame:CGRectMake(KX, heightView-48, KX, 48)];
    [button2 setBackgroundColor:[UIColor whiteColor]];
    [button2 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
    [button2 setTitle:@"收藏" forState:UIControlStateNormal];
    [button2.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
    [self.view addSubview:button2];
//
    button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button3 setFrame:CGRectMake(KX*2, heightView-48, WithView/3.0, 48)];
    [button3 setBackgroundColor:[UIColor whiteColor]];
    [button3 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
    [button3 setTitle:@"拍照" forState:UIControlStateNormal];
    [button3.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
    [self.view addSubview:button3];

    
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, heightView-48.5, WithView, 0.5f)];
    [lineView setBackgroundColor:uicolor(204, 204, 204)];
    [self.view addSubview:lineView];
    
    
    
    
    
    
    
    dispatch_queue_t imageDownloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(imageDownloadQueue, ^{
    
        
        BoxDownLoadViewController *boxDownView = [[BoxDownLoadViewController alloc] init];
        TakingpicturesViewController *camera = [[TakingpicturesViewController alloc] init];
        PhotoSelectViewController *cameraView = [[PhotoSelectViewController alloc] init];

        dispatch_async(dispatch_get_main_queue(), ^{
    
            
            
//            [camera setMcameraDeledate:self];
            [boxDownView setPlusImage:_plusImage];
            [boxDownView setMBoxDownLoadViewControllerDelegate:self];
            
            [camera.view setFrame:CGRectMake(WithView*2, 0, WithView, heightView-20)];
            [camera setMTakingpicturesViewControllerDelegate:self];
            [cameraView setPlusImage:_plusImage];
//            [cameraView setNameIndexArray:_nameArray];
//            [cameraView setImageYasuoArray:_imageArray];
            [cameraView.view setFrame:CGRectMake(0, 0, WithView, heightView-40)];
            [cameraView setMPhotoSelectViewControllerDelegate:self];
            [_mainScrollview addSubview:cameraView.view];
            [self addChildViewController:cameraView];
            //
            self.currentVC = cameraView;

            
            [_mainScrollview addSubview:camera.view];
            [self addChildViewController:camera];

            [boxDownView.view setFrame:CGRectMake(WithView, 0, WithView, heightView-20)];
            [_mainScrollview addSubview:boxDownView.view];
            [self addChildViewController:boxDownView];
//
            });
        });

    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, heightView-2.5, WithView/3.0, 2.5)];
    [bottomView setBackgroundColor:uicolor(255, 204, 0)];
    [self.view addSubview:bottomView];
    
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [_mainScrollview setBounces:NO];
    [_mainScrollview setDelegate:self];
    [button1 setTag:1];
    [button2 setTag:2];
    [button3 setTag:3];
    [button1 addTarget:self action:@selector(changeScrollView:) forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self action:@selector(changeScrollView:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addTarget:self action:@selector(changeScrollView:) forControlEvents:UIControlEventTouchUpInside];
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self.navigationController setNavigationBarHidden:YES];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float i = scrollView.contentOffset.x;
    [bottomView setFrame:CGRectMake(i*(1.0/3.0), heightView-2.5, WithView/3.0, 2.5)];
    
    if (i/WithView == 0.0)
    {
        [button1 setTitleColor:uicolor(34, 34, 34) forState:UIControlStateNormal];
        [button1.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
        
        [button2 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
        [button2.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        
        [button3 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
        [button3.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        
    }
    else if (i/WithView == 1.0)
    {
        [button2 setTitleColor:uicolor(34, 34, 34) forState:UIControlStateNormal];
        [button2.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
        
        [button1 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
        [button1.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        
        [button3 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
        [button3.titleLabel setFont:[UIFont systemFontOfSize:15.0]];

    }
    else if (i/WithView == 2.0)
    {
        [button3 setTitleColor:uicolor(34, 34, 34) forState:UIControlStateNormal];
        [button3.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
        
        [button2 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
        [button2.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
        
        [button1 setTitleColor:uicolor(153, 153, 153) forState:UIControlStateNormal];
        [button1.titleLabel setFont:[UIFont systemFontOfSize:15.0]];

    }
    
}




- (void)changeScrollView:(UIButton *)sender
{
    
    if (sender.tag == 1)
    {
        [_mainScrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    else if (sender.tag == 2)
    {
        [_mainScrollview setContentOffset:CGPointMake(WithView, 0) animated:YES];
    }
    else if (sender.tag == 3)
    {
        [_mainScrollview setContentOffset:CGPointMake(2*WithView, 0) animated:YES];
    }
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    currentX = scrollView.contentOffset.x;
//    NSLog(@"%f", currentX);
}


- (void)buttonPressPush
{
}




//素材选择
- (void)withSelectPhoto:(NSArray *)imageArray withIDarray:(NSArray *)idArray
{
    [boxImageArray removeAllObjects];
    [boxImageArray addObjectsFromArray:imageArray];
//    [boxImageArray addObjectsFromArray:PhotoImageArray];
    
    [boxIDArray removeAllObjects];
    [boxIDArray addObjectsFromArray:idArray];
//    [boxIDArray addObjectsFromArray:PhotoIDArray];
    

}



//相册选择
- (void)selectPhotoWithIMageArray:(NSArray *)imageArray withConnectIDArray:(NSArray *)IDArray
{
    [PhotoImageArray removeAllObjects];
    [PhotoImageArray addObjectsFromArray:imageArray];
//    [PhotoImageArray addObjectsFromArray:boxImageArray];
    
    [PhotoIDArray removeAllObjects];
    [PhotoIDArray addObjectsFromArray:IDArray];
//    [PhotoIDArray addObjectsFromArray:boxIDArray];
//    NSLog(@"%@", PhotoIDArray);
    
}




//通过相机进入相册
- (void)pushTakePhoto:(NSData *)imageData
{
    [_mutableArray removeAllObjects];
    [_mutableIDArray removeAllObjects];
    
    [_mutableIDArray addObjectsFromArray:PhotoIDArray];
    [_mutableIDArray addObjectsFromArray:boxIDArray];
    
    [_mutableArray addObjectsFromArray:PhotoImageArray];
    [_mutableArray addObjectsFromArray:boxImageArray];
    
    //    NSLog(@"%@", PhotoIDArray);
    if (_plusImage)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addphoto" object:_mutableArray];
        [[PostUploadImageDataBase sharedInstance] addPhotoWithImageArray:_mutableArray withConnectIDArray:_mutableIDArray];;
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [[PostUploadImageDataBase sharedInstance] openDataBase];
        [[PostUploadImageDataBase sharedInstance] insertModelwithImageArray:_mutableArray withCOnnectIDArray:_mutableIDArray];
        
        UserEditeImageViewController *editeVC = [[UserEditeImageViewController alloc] init];
        editeVC.imageMutableArray = _mutableArray;
        [self.navigationController pushViewController:editeVC animated:YES];
        
    }

}







//进入创作
- (void)BOXPush
{
    [_mutableArray removeAllObjects];
    [_mutableIDArray removeAllObjects];
    
    [_mutableIDArray addObjectsFromArray:PhotoIDArray];
    [_mutableIDArray addObjectsFromArray:boxIDArray];
    
    [_mutableArray addObjectsFromArray:PhotoImageArray];
    [_mutableArray addObjectsFromArray:boxImageArray];
    
    
        if (_plusImage)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"addphoto" object:_mutableArray];
            [[PostUploadImageDataBase sharedInstance] addPhotoWithImageArray:_mutableArray withConnectIDArray:_mutableIDArray];;
    
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            [[PostUploadImageDataBase sharedInstance] openDataBase];
            [[PostUploadImageDataBase sharedInstance] insertModelwithImageArray:_mutableArray withCOnnectIDArray:_mutableIDArray];
    
            UserEditeImageViewController *editeVC = [[UserEditeImageViewController alloc] init];
            editeVC.imageMutableArray = _mutableArray;
            [self.navigationController pushViewController:editeVC animated:YES];
            
        }
//
}


- (void)photoPUsh
{

    [_mutableArray removeAllObjects];
    [_mutableIDArray removeAllObjects];
    
    [_mutableIDArray addObjectsFromArray:PhotoIDArray];
    [_mutableIDArray addObjectsFromArray:boxIDArray];
    
    [_mutableArray addObjectsFromArray:PhotoImageArray];
    [_mutableArray addObjectsFromArray:boxImageArray];

//    NSLog(@"%@", PhotoIDArray);
        if (_plusImage)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"addphoto" object:_mutableArray];
            [[PostUploadImageDataBase sharedInstance] addPhotoWithImageArray:_mutableArray withConnectIDArray:_mutableIDArray];;
    
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            [[PostUploadImageDataBase sharedInstance] openDataBase];
            [[PostUploadImageDataBase sharedInstance] insertModelwithImageArray:_mutableArray withCOnnectIDArray:_mutableIDArray];
    
            UserEditeImageViewController *editeVC = [[UserEditeImageViewController alloc] init];
            editeVC.imageMutableArray = _mutableArray;
            [self.navigationController pushViewController:editeVC animated:YES];
    
        }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
